package cn.hackcloud.netty.protocol.netty.server;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import cn.hackcloud.netty.protocol.netty.domain.Header;
import cn.hackcloud.netty.protocol.netty.domain.MessageType;
import cn.hackcloud.netty.protocol.netty.domain.NettyMessage;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class LoginAuthRespHandler extends ChannelHandlerAdapter {
	private Map<String, Object> nodeCheck = new ConcurrentHashMap<String, Object>();
	private String[] whiteList = { "127.0.0.1", "192.168.31.100" };

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		NettyMessage message = (NettyMessage) msg;
		if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_REQ.value()) {
			String nodeIndex = ctx.channel().remoteAddress().toString();
			NettyMessage loginResp = null;
			if (nodeCheck.containsKey(nodeIndex)) {
				loginResp = buildResponse((byte) -1);
			} else {
				InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
				String ip = address.getAddress().getHostAddress();
				boolean isOk = false;
				for (String wip : whiteList) {
					if (wip.equals(ip)) {
						isOk = true;
						break;
					}
				}
				loginResp = isOk ? buildResponse((byte) 0) : buildResponse((byte) -1);
				if (isOk) {
					nodeCheck.put(nodeIndex, true);
				}

			}
			System.out.println("The login response is:" + loginResp + " body[" + loginResp.getBody() + "]");
			ctx.writeAndFlush(loginResp);
		} else {
			ctx.fireChannelRead(msg);
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		ctx.close();
		ctx.fireExceptionCaught(cause);
	}

	public NettyMessage buildResponse(byte result) {
		NettyMessage message = new NettyMessage();
		Header header = new Header();
		header.setType(MessageType.LOGIN_RESP.value());
		message.setHeader(header);
		message.setBody(result);
		return message;
	}
}
